<?php
/**
 * @file cmstags.php
 * @author CaoQiCheng
 * @date 2017年2月7日 下午17:16:11
 * @brief 
 */

namespace haibao\jiepai\data;

class Tags extends \haibao\jiepai\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\jiepai\model\data\tag\CmsTags');
    }
    
    public function getTagsList($query){
    	return $this->getAll($query);
    }
    
    public function getTagNameByTagId($tagIds){
        $str = preg_replace('/(\d+)/', '%s', implode(',', $tagIds));
        $sql = "SELECT TagID,TagName,CategoryID FROM cms_tags WHERE TagID in (".$str.")";
        $result = $this->query($sql, $tagIds);
        $tagInfo = array();
        while ($row = $result->fetch_assoc()){
            $tagInfo[$row['TagID']] = $row;
        }
        return $tagInfo;
    }
    public function getOneTag($query){
    	return $this->getOne($query);
    }
    
    public function saveTags($tagsModel,$relationWord){
    	$this->beginTranscation();
    	try {
    		if($tagsModel){
    			if($tagsModel->Id){
    				$this->save($tagsModel);
    			}else {
    				$this->add($tagsModel);
    			}
	    		if(!empty($relationWord)){
	    			$this->saveTagsRelation($tagsModel->Id, $relationWord);
	    		}
    		}
    	}catch (\Exception $e) {
    		$this->rollback();
    		throw $e;
    	}
    	$this->endTranscation();
    		
    	return $tagsModel;
    }
    /**
     * 根据标签id 删除标签
     */
    public function delTagById($tagId){
    	$sqlUpdate = "update cms_tags set Flag = -1 where TagID=%s";
    	$this->query($sqlUpdate,array($tagId));
    	$sql = 'delete from cms_tags_relation where TagID = %s';
    	$this->query($sql,array($tagId));
    	$sql = 'delete from cms_tags_relation where TagID2 = %s';
    	$this->query($sql,array($tagId));
    }
    
    /**
     * 根据标签查数据
     */
     public function getTagsByCategory($category){
     	$tagList = array();
     	$sql = "SELECT TagName from cms_tags where Flag = 0 AND CategoryID=%s";
     	$result = $this->query($sql,array($category));
     	while ($row = $result->fetch_assoc()){
     		array_push($tagList, $row['TagName']);
     	}
     	return $tagList;
     }
    
    /**
     * 增加相关人物
     */
    public function saveTagsRelation($tagId,$relationWord){
    	if(!$tagId){
    		return false;
    	}
    	$this->query('delete from `cms_tags_relation` where TagID = %s or TagID2 = %s', array(
    			$tagId,$tagId
    	));
    	if(isset($relationWord) && $relationWord){
    		$sql = 'insert into cms_tags_relation(TagID,RelationID,RelationLevel,TagID2,Weight) values ';
	
    		$arrSql = $arrValues = array();
    		$tagId2Arr = array();
    		
    		foreach ($relationWord as $key=>$val){
    			if($key==\haibao\jiepai\model\data\tag\CmsTags::CATEGORY_RELATION){
    				$tagId2Arr = explode(',',$val);
    				foreach($tagId2Arr as $tagId2){
    					array_push($arrSql, '(%s,%s,%s,%s,%s)');
    					array_push($arrValues, (int)$tagId);
    					array_push($arrValues, 0);
    					array_push($arrValues, '≈');
    					array_push($arrValues, (int)$tagId2);
    					array_push($arrValues, 10000);
    					
    					array_push($arrSql, '(%s,%s,%s,%s,%s)');
    					array_push($arrValues, (int)$tagId2);
    					array_push($arrValues, 0);
    					array_push($arrValues, '≈');
    					array_push($arrValues, (int)$tagId);
    					array_push($arrValues, 10000);
    				}
    			}elseif($key==\haibao\jiepai\model\data\tag\CmsTags::CATEGORY_SAMEWORD){
	    				$tagId2Arr = explode(',',$val);
	    				foreach($tagId2Arr as $tagId2){
	    					array_push($arrSql, '(%s,%s,%s,%s,%s)');
	    					array_push($arrValues, (int)$tagId);
	    					array_push($arrValues, 0);
	    					array_push($arrValues, '=');
	    					array_push($arrValues, (int)$tagId2);
	    					array_push($arrValues, 10000);
	    					
	    					array_push($arrSql, '(%s,%s,%s,%s,%s)');
	    					array_push($arrValues, (int)$tagId2);
	    					array_push($arrValues, 0);
	    					array_push($arrValues, '=');
	    					array_push($arrValues, (int)$tagId);
	    					array_push($arrValues, 10000);
    				}
    			}elseif ($key==\haibao\jiepai\model\data\tag\CmsTags::CATEGORY_TOPWORD){
    				$tagId2Arr = explode(',',$val);
    				foreach($tagId2Arr as $tagId2){
    					array_push($arrSql, '(%s,%s,%s,%s,%s)');
    					array_push($arrValues, (int)$tagId);
    					array_push($arrValues, 0);
    					array_push($arrValues, '<');
    					array_push($arrValues, (int)$tagId2);
    					array_push($arrValues, 10000);
    					
    					array_push($arrSql, '(%s,%s,%s,%s,%s)');
    					array_push($arrValues, (int)$tagId2);
    					array_push($arrValues, 0);
    					array_push($arrValues, '>');
    					array_push($arrValues, (int)$tagId);
    					array_push($arrValues, 10000);
    				}
    			}elseif ($key==\haibao\jiepai\model\data\tag\CmsTags::CATEGORY_DOWNWORD){
    				$tagId2Arr = explode(',',$val);
    				foreach($tagId2Arr as $tagId2){
    					array_push($arrSql, '(%s,%s,%s,%s,%s)');
    					array_push($arrValues, (int)$tagId);
    					array_push($arrValues, 0);
    					array_push($arrValues, '>');
    					array_push($arrValues, (int)$tagId2);
    					array_push($arrValues, 10000);
    					
    					array_push($arrSql, '(%s,%s,%s,%s,%s)');
    					array_push($arrValues, (int)$tagId2);
    					array_push($arrValues, 0);
    					array_push($arrValues, '<');
    					array_push($arrValues, (int)$tagId);
    					array_push($arrValues, 10000);
    				}
    			}
    		}
    	}
    		if($arrSql && $arrValues){
    			$sql .= implode(',', $arrSql);
    			$this->query($sql, $arrValues);
    		}
    		
    	
    }
    
    
}